<!DOCTYPE html>
<html class="x-admin-sm">

    <head>
        <meta charset="UTF-8">
        <title>媒体关联管理</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<!--        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />-->
        <link rel="stylesheet" href="./css/font.css">
        <link rel="stylesheet" href="./css/xadmin.css">
        <script src="./lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="./js/xadmin.js"></script>
        <script type="text/javascript" src="./js/jquery.min.js"></script>
        <script type="text/javascript" src="./data/sensor.js"></script>
        <!--[if lt IE 9]>
          <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
          <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->

    </head>

    <body>
        <div class="x-nav">
            <span class="layui-breadcrumb">
<!--                <a href="">首页</a>-->
<!--                <a href="">媒体管理</a>-->
                <a>
                    <cite>此资源关联的设备地点</cite></a>
            </span>
            <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
                <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
            </a>
        </div>
        <div class="layui-fluid">

                <div class="layui-col-md12">
                    <div class="layui-card">

                        <div class="layui-card-body ">
                               全选：<input type="checkbox" id="checkAll" onchange="changeChecked(this)">
                               <ul id="ul_led">

                               </ul>
                            <button class="layui-btn layui-btn-sm"  onclick="saveInfo()"  > 保存</button>
                        </div>
                    </div>
                </div>
        </div>
    </body>

    <script type="text/html" id="toolbarDemo">


        <div class = "layui-btn-container" >

<!--            <button class = "layui-btn layui-btn-sm" lay-event = "getCheckData" > 获取选中行数据 </button>-->
<!--            <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button > -->
<!--            <button class = "layui-btn layui-btn-sm" lay-event = "isAll" > 验证是否全选</button>-->
            <button class = "layui-btn layui-btn-danger"   lay-event = "delete" > 删除</button>
            <button class="layui-btn layui-btn-sm"  lay-event = "editInfo" > 修改</button>

        </div >
    </script>



    <script>


    </script>

    <script type="text/javascript">

        function getQueryVariable(variable)
        {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i=0;i<vars.length;i++) {
                var pair = vars[i].split("=");
                if(pair[0] == variable){return pair[1];}
            }
            return(false);
        }

        function initSensorList(dataSensor){

            //如果不是管理员
            let toolbarInfo='#toolbarMenu';
            //如果不是管理员
            if(!sensorServer.getRoleIsAdmin()){
                toolbarInfo='';
            }

            layui.use('table', function () {
                var table = layui.table;
                //第一个实例
                table.render({
                    elem: '#demo'
                    //,
                    //height: 450
                    , data: dataSensor,
                    page: false //开启分页
                    ,limit: 100,
                    toolbar:toolbarInfo,// '#toolbarMenu',
                    cols: [[ //表头
                        // {field:"id", checkbox:true,fixed: 'left'},
                        {field: 'id', title: '编号', width: 80, sort: true, fixed: 'left'}
                        , {field: 'resourcesType', title: '媒体类型', width: 150}
                        , {field: 'resourcesUrl', title: '内容', width: 150, edit: 'text'}
                        , {field: 'resourcesName', title: '名称', width: 150, edit: 'text'}
                        , {field: 'txtContent', title: '文本内容'}
                        , {field: '', title: '编辑', width: 150, sort: true, toolbar:'#toolbarDemo'}
                    ]]
                    ,loading:true,
                    done:function (res, curr, count) {
                        $("[data-field='resourcesType']").children().each(function(){
                            if ($(this).text() != '媒体类型')  {
                                if ($(this).text() == '1') {
                                    $(this).text("文本");
                                }
                                if ($(this).text() == '2') {
                                    $(this).text("图片");
                                }
                                if ($(this).text() == '3') {
                                    $(this).text("视频");
                                }
                            }
                        });
                    }
                });

                //监听单元格编辑
                table.on('edit(test_table)',
                    function (obj) {
                        //如果不是管理员
                        if(!sensorServer.getRoleIsAdmin()){
                            alert("无权限修改");
                            var oldText = $(this).prev().text();
                            $(this).val(oldText);
                            return;
                        }
                        var value = obj.value //得到修改后的值
                            ,
                            data = obj.data //得到所在行所有键值
                            ,
                            field = obj.field; //得到字段
                        layer.msg('[ID: ' + data.pid + '] ' + field + ' 字段更改为：' + value);
                        //updateTable(data.pid,value);
                        var jsonResult = sensorServer.updateLedInfo(obj.data);
                        console.log(jsonResult);
                    });

                //监听行单击事件（双击事件为：rowDouble）
                //table.on('row(test_table)', function(obj){
                // var data = obj.data;
                // layer.alert(JSON.stringify(data), {
                //     title: '当前行数据：'
                // });

                //标注选中样式
                //obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
                //  });

                //头工具栏事件
                table.on('toolbar(test_table)', function(obj) {
                    var checkStatus = table.checkStatus(obj.config.pid);
                    switch (obj.event) {
                        case 'delete':
                            layer.msg('data:'+obj.data.pid);
                            //del(obj);
                            break;
                        case 'delCheckData':
                            // var data = checkStatus.data;
                            // layer.alert(JSON.stringify(data));
                            // break;
                            layer.confirm('确认要删除吗？',function(index){
                                for(let i=0;i<obj.config.data.length;i++)
                                {
                                    if(obj.config.data[i].LAY_CHECKED){
                                        sensorServer.delLedInfo(obj.config.data[i]);
                                        layer.msg('已删除!',{icon:1,time:1000});
                                    }
                                }
                                location.reload();
                            });
                            break;
                    };
                });
                //列里的工具栏
                table.on('tool(test_table)',
                    function(obj) {
                        //如果不是管理员
                        if(!sensorServer.getRoleIsAdmin()){
                            alert("无权限修改");
                            var oldText = $(this).prev().text();
                            $(this).val(oldText);
                            return;
                        }
                        //var checkStatus = table.checkStatus(obj.config.pid);
                        switch (obj.event) {
                            case 'delete':
                                layer.msg('data:'+obj.data.pid);
                                del(obj);
                                break;
                            case 'editInfo':
                                xadmin.open('修改资源','./ledContentResourcesList-edit.html'+"?id="+obj.data.id,600,400);
                                break;
                        };
                    });
            });
        }

        function changeChecked(val) {
            if(val.checked) {
                $("#ul_led li input[type='checkbox']").attr("checked", true);
            }else{
                $("#ul_led li input[type='checkbox']").attr("checked", false);
            }
        }

        function getData(){
             dataSensor=sensorServer.getLedAllContentResourcesList({"page":1,"rows":1000});
             return dataSensor;
        }
        var resourcesId=0;

        //加载默认数据
        $(function () {
            // var  dataSensor=getData();
            // if (dataSensor!=null&&dataSensor!=undefined){
            //     initSensorList(dataSensor.data);
            // }else{
            //     layer.msg('加载失败！!',{icon:1,time:1000});
            // }
            resourcesId=getQueryVariable("resourcesId");
            let ledDataResult=sensorServer.getLedList({"page":1,"rows":1000});
            let ledList=ledDataResult.data;

            let ledContentResult=sensorServer.getLedContentList({"page":1,"rows":1000});
            let ledContentList=ledContentResult.data;
            debugger;
            for (let i=0;i<ledList.length;i++){
                  let ledInfo=ledList[i];
                  let checked=isChecked(ledInfo.pid,ledContentList);
                  let isCheckIdStr="";
                  if(checked){
                      isCheckIdStr="checked='checked'";
                  }
                  let listr="<li><input data-pid='"+ledInfo.pid+"' type='checkbox' "+isCheckIdStr+"'>  "+ledInfo.deviceLocation+"</li>"
                  $("#ul_led").append(listr);
            }

        });

        function isChecked(ledPid,ledContentList) {
            for (let i=0;i<ledContentList.length;i++){
                if(ledPid==ledContentList[i].ledPid&&resourcesId==ledContentList[i].ledResourcesId)
                    return true;
            }
           return false;
        }

        function sensorInfo(pid,deviceId){
            this.pid=pid;
            this.deviceId=deviceId;
        }
        function  del(obj){
            layer.confirm('确认要删除吗？',function(index){
                //发异步删除数据
               sensorServer.delLedContentResources(obj.data);
                $(obj).parents("tr").remove();
                $(obj).remove();
                layer.msg('已删除!',{icon:1,time:1000});
                location.reload();
            });
        }
        //保存数据
        function  saveInfo() {
            //最新的全部选中的
            let ledPids=[];
            $("#ul_led li input[type='checkbox']").each(function() {
                let ledPID=this.dataset.pid;
                if(this.checked){
                    ledPids.push(ledPID);
                }
            });
            let params={"resourceId":resourcesId,"ledPids":ledPids};
            let result=sensorServer.updateResourcesLed(params);
            if(result==1){
                layer.msg("保存成功！");
            }else{
                layer.msg("保存失败！");
            }
        }
    </script>



</html>
